package br.ucb.sigplan.action.impl;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.ucb.sigplan.action.Action;
import br.ucb.sigplan.beans.Conteudo;
import br.ucb.sigplan.crud.ConteudoCrud;
import br.ucb.sigplan.validator.Validator;

public class VerificaConteudoTurmaAction implements Action {

	@SuppressWarnings("unchecked")
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<String> erros = Validator.validaConteudosTurma(req);
		System.out.println("Conteudo = "+req.getParameter("conteudo"));
		if (erros.isEmpty()) {
			if (req.getSession().getAttribute("conteudosTurma") == null) {
				req.getSession().setAttribute("conteudosTurma", new HashSet<Conteudo>());
			}
			Set<Conteudo> conteudosTurma = (Set<Conteudo>) req.getSession().getAttribute("conteudosTurma"); 
			conteudosTurma.add(ConteudoCrud.findById(new Integer(req.getParameter("conteudo"))));
		}
		else {
			req.setAttribute("erros", erros);
		}
		return "/jsp/professor/conteudo/profCadastraConteudoTurma.jsp";
	}

}
